package com.ym.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ym.response.Result;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import com.ym.service.SysUserService;
import com.ym.entity.SysUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;

@RestController
@RequestMapping("/sysUser")
@Tag(name = "SysUser管理", description = "与SysUser相关的API")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 根据 ID 查询
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询SysUser", description = "返回指定ID的SysUser信息")
    @ApiResponse(responseCode = "200", description = "成功返回SysUser信息")
    public Result<SysUser> getById(@PathVariable Long id) {
        SysUser result = sysUserService.getById(id);
        if (result != null) {
            return Result.ok(result);
        } else {
            return Result.fail(404, "未找到对应的SysUser"); // 返回失败信息
        }
    }

    /**
     * 新增数据
     */
    @PostMapping("/save")
    @Operation(summary = "新增SysUser", description = "创建一个新的SysUser")
    @ApiResponse(responseCode = "200", description = "成功新增SysUser")
    public Result<Void> save(@RequestBody SysUser sysUser) {
        boolean success = sysUserService.save(sysUser);
        if (success) {
            return Result.ok();
        } else {
            return Result.fail(500, "新增失败");
        }
    }

    /**
     * 更新数据
     */
    @PutMapping("/update")
    @Operation(summary = "更新SysUser", description = "更新指定ID的SysUser信息")
    @ApiResponse(responseCode = "200", description = "成功更新SysUser")
    public Result<Void> update(@RequestBody SysUser sysUser) {
        boolean success = sysUserService.updateById(sysUser);
        if (success) {
            return Result.ok();
        } else {
            return Result.fail(500, "更新失败");
        }
    }

    /**
     * 删除数据
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除SysUser", description = "删除指定ID的SysUser")
    @ApiResponse(responseCode = "200", description = "成功删除SysUser")
    public Result<Void> delete(@PathVariable Long id) {
        boolean success = sysUserService.removeById(id);
        if (success) {
            return Result.ok();
        } else {
            return Result.fail(500, "删除失败");
        }
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询SysUser", description = "分页获取SysUser列表，支持条件查询")
    @ApiResponse(responseCode = "200", description = "成功返回分页结果")
    public Result<Page<SysUser>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @ModelAttribute SysUser queryCondition) {
        Page<SysUser> page = new Page<>(current, size);
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        //根据情况进一步封装wrapper
        Page<SysUser> result = sysUserService.page(page, wrapper);
        return Result.ok(result);
    }
}